using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Telerik.WebControls;
/// <summary>
/// Summary description for MyCustomFilteringColumn
/// </summary>
public class MyCustomFilteringColumn : GridBoundColumn
{
    //RadGrid will call this method when it initializes the controls inside the filtering item cells
    protected override void SetupFilterControls(TableCell cell)
    {
        base.SetupFilterControls(cell);
        cell.Controls.RemoveAt(0);

        RadComboBox list = new RadComboBox();
        list.ID = "list" + this.DataField;

        list.ShowToggleImage = false;
        list.EnableLoadOnDemand = true;
        list.MarkFirstMatch = true;
        list.AutoPostBack = true;
        list.Height = Unit.Pixel(100);

        list.ItemsRequested += new RadComboBoxItemsRequestedEventHandler(this.list_ItemsRequested);
        list.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(list_SelectedIndexChanged);

        list.OnClientItemsRequesting = "OnClientItemsRequesting";

        cell.Controls.AddAt(0, list);
        cell.Controls.RemoveAt(1);

    }

    void list_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    {
        RadComboBox list = sender as RadComboBox;

        list.DataTextField = this.DataField;
        list.DataValueField = this.DataField;

        list.DataSource = DAL.GetData("SELECT DISTINCT " + this.UniqueName + " FROM Rank WHERE " + this.UniqueName + " LIKE '" + e.Text + "%' order by " + this.UniqueName + " ASC");
        list.DataBind();
    }

    void list_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        GridFilteringItem filterItem = (sender as RadComboBox).NamingContainer as GridFilteringItem;

        this.SelectedValue = (sender as RadComboBox).SelectedValue;
        this.FilteredColumnName = this.UniqueName;

        if (this.UniqueName == "ID" || this.UniqueName == "Score")
        {
            //this is example for filtering for integer column type
            filterItem.FireCommandEvent("Filter", new Pair("EqualTo", this.UniqueName));
        }
        //filtering for string column type
        filterItem.FireCommandEvent("Filter", new Pair("Contains", this.UniqueName));

    }
    public string SelectedValue
    {
        get
        {
            object res = this.Owner.OwnerGrid.Page.Session["SelectedValue"];
            if (res != null)
            {
                return (string)res;
            }
            return "";
        }
        set
        {
            this.Owner.OwnerGrid.Page.Session["SelectedValue"] = value;
        }
    }
    public string FilteredColumnName
    {
        get
        {
            object res = this.Owner.OwnerGrid.Page.Session["FilteredColumnName"];
            if (res != null)
            {
                return (string)res;
            }
            return "";
        }
        set
        {
            this.Owner.OwnerGrid.Page.Session["FilteredColumnName"] = value;
        }
    }
    //RadGrid will call this method when the value should be set to the filtering input control(s)
    protected override void SetCurrentFilterValueToControl(TableCell cell)
    {
        base.SetCurrentFilterValueToControl(cell);

        RadComboBox list = (RadComboBox)cell.Controls[0];
        if (this.CurrentFilterValue != string.Empty)
        {
            list.SelectedValue = this.CurrentFilterValue;
        }
    }

    //RadGrid will call this method when the filtering value should be extracted from the filtering input control(s)
    protected override string GetCurrentFilterValueFromControl(TableCell cell)
    {
        RadComboBox list = (RadComboBox)cell.Controls[0];

        return list.SelectedValue;
    }
    public override GridColumn Clone()
    {
        return base.Clone();
    }
    protected override string GetFilterDataField()
    {
        return this.DataField;
    }

}
